package com.poi.java.util;

public class ToChUtils {

    private void ToChUtils() {
    }

    /**
     * 将yyyy-M-d 格式日期转换成中文格式
     * 例：2000-1-1 --> 二〇〇〇年一月一日
     */
    public static String toCh(String date) {
        if (date == null) {
            return date;
        }

        try {
            String[] dateArray = date.split("-");
            StringBuilder chineseDate = new StringBuilder();
            for (int i = 0; i < dateArray.length; i++) {
                chineseDate.append(formatDigit(dateArray[i]));
                if (i == 0) {
                    chineseDate.append("年");
                }

                if (i == 1) {
                    chineseDate.append("月");
                }

                if (i == 2) {
                    chineseDate.append("日");
                }
            }

            return chineseDate.toString();
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 截取后的年月日转换为中文
     * 例1：2000 --> 二〇〇〇
     * 例1：10 --> 十
     */
    public static String formatDigit(String sign) {
        if (sign == null) {
            return null;
        }

        char[] signChar = sign.toCharArray();
        StringBuilder chinese = new StringBuilder();
        if (signChar.length > 3 || signChar.length < 2) {
            for (char c : signChar) {
                chinese.append(ChineseNumberEnum.getValue(String.valueOf(c)));
            }
        } else if (signChar.length ==2 && String.valueOf(signChar[0]).equals("0")) {
            chinese.append(ChineseNumberEnum.getValue(String.valueOf(signChar[1])));
        } else {
            chinese.append(ChineseNumberEnum.getValue(sign));
        }

        return chinese.toString();
    }

    public static void main(String[] args) {
        System.out.println(toCh("2023-05-15"));
    }
}
